package cn.dujc.superkid.fragment.main;



import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import cn.dujc.core.ui.BaseFragment;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.activities.ErrorQueActivity;
import cn.dujc.superkid.activities.GoodQueActivity;
import cn.dujc.superkid.activities.InforUserActivity;
import cn.dujc.superkid.activities.LoginActivity;
import cn.dujc.superkid.activities.MeExamMessage;
import cn.dujc.superkid.activities.MeRecordActivity;
import cn.dujc.superkid.activities.MessageActivity;
import cn.dujc.superkid.activities.MyExamActivity;
import cn.dujc.superkid.activities.MyCourseActivity;
import cn.dujc.superkid.activities.MyQuestionActivity;
import cn.dujc.superkid.activities.MyVideoMesssge;
import cn.dujc.superkid.activities.SettingActivity;
import cn.dujc.superkid.activities.StartActivity;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.IsSignInResult;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.bean.SignInResult;
import cn.dujc.superkid.bean.StartResult;
import cn.dujc.superkid.utils.ImageLoadHelper;
import cn.dujc.superkid.utils.Preferences;


/**
 * A simple {@link Fragment} subclass.
 */
// TODO: 2018/8/16 0016 连续打卡第一天后台返回天数0？，待确定第二天连续签到天数
public class MeFragment extends BaseFragment implements View.OnClickListener {
    private final String  TAG  = MeFragment.class.getName();
    private TextView textView,tv_daka,tv_xm,level,share;
    private View mHeaderView;
    private ImageView iv_tx;
    private ImageView signin;
    private LinearLayout myExam;
    private LinearLayout me__my_course;
    private LinearLayout goodQue;
    private LinearLayout errorQue;
    private LinearLayout me_my_que;
    private LinearLayout me_record;
    private LinearLayout me_video_messsge,me_exam_message;
    private TextView message;
    private  RegisterResult registerResult;

    @Override
    //关联视图
    public int getViewId() {
        return R.layout.fragment_me1;
    }

    @Override
    public View createRootView(View view) {
        return frameRootView(view);
    }
    //设置actionbar
    @Nullable
    @Override
    public Toolbar initToolbar(ViewGroup parent) {
        final Toolbar toolbar = (Toolbar) LayoutInflater.from(parent.getContext()).inflate(R.layout.toolbar_mine, parent, false);
        toolbar.findViewById(R.id.toolbar_menu_id).setOnClickListener(this);
        TextView title = toolbar.findViewById(R.id.toolbar_title_id);
        title.setText("我的");
        getData();
        return toolbar;

    }
    String path="/sdcard/myHead/";
    public void onResume() {
        super.onResume();
        judgement();
        Bitmap bt = BitmapFactory.decodeFile(path + "head.jpg");
        if (bt != null) {
            @SuppressWarnings("deprecation")
            Drawable drawable = new BitmapDrawable(bt);// 转换成drawable
            iv_tx.setImageDrawable(drawable);
        }
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        registerResult = Preferences.getInstance().getLoginResult(mActivity);
        me__my_course=(LinearLayout) findViewById(R.id.me__my_course);
        tv_xm =(TextView)findViewById(R.id.tv_xm);
        level = (TextView)findViewById(R.id.level) ;
        iv_tx = (ImageView)findViewById(R.id.iv_tx);
        textView = (TextView) findViewById(R.id.startbang);
        signin = (ImageView) findViewById(R.id.me_signin);
        myExam = (LinearLayout) findViewById(R.id.me_my_exam);
        goodQue = (LinearLayout) findViewById(R.id.me_good_que);
        errorQue = (LinearLayout) findViewById(R.id.me_error_que);
        me_my_que = (LinearLayout) findViewById(R.id.me_my_que);
        me_record = (LinearLayout) findViewById(R.id.me_record);
        me_video_messsge = (LinearLayout) findViewById(R.id.me_video_messsge);
        me_exam_message = (LinearLayout) findViewById(R.id.me_exam_message);
        message = (TextView)findViewById(R.id.me_message);
        me_video_messsge.setOnClickListener(this);
        me_exam_message.setOnClickListener(this);
        iv_tx.setOnClickListener(this);
        me_record.setOnClickListener(this);
        message.setOnClickListener(this);
        errorQue.setOnClickListener(this);
        goodQue.setOnClickListener(this);
        myExam.setOnClickListener(this);
        textView.setOnClickListener(this);
        signin.setOnClickListener(this);
        me__my_course.setOnClickListener(this);
        me_my_que.setOnClickListener(this);
        judgement();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //设置
            case R.id.toolbar_menu_id:{
                if (!Preferences.isLogin(this)) {
                    starter().go(LoginActivity.class,true);
                }else {
                    starter().go(SettingActivity.class);
                }
            }break;
            //case R.id.xxx:{}break;
            case R.id.startbang:{
                if (!Preferences.isLogin(this)) {
                    starter().go(LoginActivity.class,true);
                }else {
                    starter().go(StartActivity.class);
                }
            }break;
            case R.id.me_signin:
                qiandao();
                break;
            case R.id.me__my_course:
                if (!Preferences.isLogin(this)) {
                    starter().go(LoginActivity.class,true);
                }else {
                    starter().go(MyCourseActivity.class);
                }
                break;
            case R.id.me_my_exam://我的考试
                if (!Preferences.isLogin(this)) {
                    starter().go(LoginActivity.class,true);
                }else {
                    starter().go(MyExamActivity.class);
                }
                break;
            case R.id.me_good_que://好题本
                if (!Preferences.isLogin(this)) {
                    starter().go(LoginActivity.class,true);
                }else {
                    starter().go(GoodQueActivity.class);
                }
                break;
            case R.id.me_error_que:
                if (!Preferences.isLogin(this)) {
                    starter().go(LoginActivity.class,true);
                }else {
                    starter().go(ErrorQueActivity.class);
                }
                break;
            case R.id.me_message://我的消息
                if (!Preferences.isLogin(this)) {
                    starter().go(LoginActivity.class,true);
                }else {
                    starter().go(MessageActivity.class);
                }
                break;
            case R.id.iv_tx:
                if (!Preferences.isLogin(this)) {
                    starter().go(LoginActivity.class,true);
                }else {
                    starter().go(InforUserActivity.class);
                }
                break;
            case R.id.me_my_que://我的疑问
                if (!Preferences.isLogin(this)) {
                    starter().go(LoginActivity.class,true);
                }else {
                    starter().go(MyQuestionActivity.class);
                }
                break;
            case R.id.me_record://学习记录
                if (!Preferences.isLogin(this)) {
                    starter().go(LoginActivity.class,true);
                }else {
                    starter().go(MeRecordActivity.class);
                }
                break;
            case R.id.me_video_messsge://我的视频留言
                if (!Preferences.isLogin(this)) {
                    starter().go(LoginActivity.class,true);
                }else {
                    starter().go(MyVideoMesssge.class);
                }
                break;
            case R.id.me_exam_message://我的考试留言
                if (!Preferences.isLogin(this)) {
                    starter().go(LoginActivity.class,true);
                }else {
                    starter().go(MeExamMessage.class);
                }
                break;
            default:break;
        }
    }

    private void getData(){
        if (!Preferences.isLogin(this)) {
            starter().go(LoginActivity.class,true);
        }else {
            final RegisterResult loginResult = Preferences.getInstance().getLoginResult
                    (mActivity);
            HttpHelper.getUserCenter().memberIsSignIn(QMap.build()
                    .put("user_id", loginResult.getUid())
                    .put("logincode", loginResult.getLogincode())
                    .create()).enqueue(new ResultCallBack<Result<IsSignInResult>>(mActivity) {
                @Override
                public void onSuccessCallback(int code, Result<IsSignInResult> body) {
                    final IsSignInResult data = body.getData();
                    if (data != null) {
                        if(data.getIs_sign_in().equals("1")){//1未签到
                            signin.setImageResource(R.mipmap.da_qia_qian_dao);
                        }else if (data.getIs_sign_in().equals("2")){//2已签到
                            signin.setImageResource(R.mipmap.da_qian_qian_dao_disable);
                        }
                    }
                }

                @Override
                public void onFailureCallback(int code, String msg) {
                    super.onFailureCallback(code, msg);
                    ToastUtil.showToast(mActivity,msg);
                }
            });
        }

    }
    private void numberOfStarAndLevel(){
        HttpHelper.getUserCenter().memberStar(QMap.build()
                .put("logincode", registerResult.getLogincode())
                .put("channel",  Preferences.getInstance().getChannel(mActivity).getChannel_id())
                .put("user_id", Integer.parseInt(registerResult.getUid()))
                .create()).enqueue(new ResultCallBack<Result<StartResult>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<StartResult> body) {
                if (body!=null){
                    level.setText(body.getData().getLevel());
                    Log.i(TAG,"======caizhijia==level===="+ body.getData().getLevel());
                }

            }
        });
    }
    private void judgement(){
        if (!Preferences.isLogin(this)) {
            tv_xm.setText("--.--");
            level.setText("--.--");
        }else {
            tv_xm.setText(registerResult.getNicename());
            ImageLoadHelper.loadImage(iv_tx,registerResult.getAvatar());
            numberOfStarAndLevel();
        }
    }

    private void qiandao() {
        if (!Preferences.isLogin(this)) {
            starter().go(LoginActivity.class,true);
        }else {
            final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);
            HttpHelper.getUserCenter().memberSignIn(QMap.build()
                    .put("user_id", loginResult.getUid())
                    .put("logincode", loginResult.getLogincode())
                    .create()).enqueue(new ResultCallBack<Result<SignInResult>>(mActivity) {
                @Override
                public void onSuccessCallback(int code, Result<SignInResult> body) {
                    final SignInResult data = body.getData();
                    if(code==200){
                        dialog(data.getNum());
                    }
                }
                @Override
                public void onFailureCallback(int code, String msg) {
                    super.onFailureCallback(code, msg);
                }
            });
        }
    }

    private void dialog(String getNum) {
        final Dialog dialog = new Dialog(getContext(), R.style.MyDialog);
        View view = LayoutInflater.from(this.getActivity()).inflate(R.layout.layout_singin, null);
        tv_daka= (TextView)view.findViewById(R.id.tv_daka);
        share= (TextView)view.findViewById(R.id.share);
        tv_daka.setText("我家宝贝在小学课堂平台，已连续学习了"+getNum+"天");
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.showToast(mActivity,"分享成功");
                dialog.dismiss();
            }
        });
        dialog.setContentView(view);
        dialog.show();
        dialog.setCanceledOnTouchOutside(true);
        signin.setImageResource(R.mipmap.da_qian_qian_dao_disable);
    }


}
